import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http'
import { Observable, Subject  } from 'rxjs';

const API_URL = 'http://localhost:5187/';

export interface ApiReset<T> {
  success: boolean
  result: T
  message: string
}

export interface PageBlock {
  id: number
  type: number
  settings: string
  body: string
  updatedOn: string
  sortNo: number
}

export interface Page {
  id: number
  title: string
  createdBy: number
  createdOn: string
  tags: string
  updatedOn: string
  blocks: PageBlock[]
}



@Injectable({
  providedIn: 'root'
})
export class MypageService {

  constructor(private httpClient: HttpClient) { }
  public get(id: number): Observable<Page> {
    let url = `${API_URL}/api/Page/${id}`;
    let rs = this.httpClient.get(url) as Observable<ApiReset<Page>>;  
    var myObservable = new Subject<Page>();
    rs.subscribe(x=> {
      myObservable.next(x.result);
      myObservable.complete();
    });
    
    return myObservable.asObservable();
  }

  public saveBlock(block:PageBlock): Observable<string> {
    let url = `${API_URL}/api/page/saveblock`;    
    let rs = this.httpClient.post(url, block) as Observable<ApiReset<string>>;  
    var myObservable = new Subject<string>();
    rs.subscribe(x=> {
      myObservable.next(x.result);
      myObservable.complete();
    });
    
    return myObservable.asObservable();
  }

}
